package com.af.community.smart.business

import android.os.Bundle
import com.af.community.smart.base.BaseActivity
import com.af.community.smart.base.getString
import com.af.community.smart.base.oneClick
import com.af.community.smart.utils.ActivityStack
import com.af.community.smart.utils.bindLifeCycle
import com.af.community.smart.utils.subscribeByNoError
import com.af.community.smart.utils.trimString
import com.af.community.smart.viewmodel.DailyViewModel
import com.jeremyliao.liveeventbus.LiveEventBus
import kotlinx.android.synthetic.main.activity_daily_add.*
import org.jetbrains.anko.toast
import org.koin.androidx.viewmodel.ext.android.getViewModel

class DailyAddActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_daily_add)
        initTitle(title = "新增日志")
    }

    override fun getViewModel() = getViewModel<DailyViewModel>()

    override fun initTitle(title: String, name: String) {
        super.initTitle(title, name)
        bt_submit.oneClick {
            if (daily_input.text.isBlank()) {
                daily_input.requestFocus()
                toast("请输入内容！")
                return@oneClick
            }

            getViewModel().getAddData(
                "userId" to getString("userId"),
                "content" to daily_input.trimString()
            )
                .bindLifeCycle(getLifecycleOwner())
                .subscribeByNoError(
                    event = getViewModel(),
                    onSuccess = {
                        LiveEventBus.get("daily_add").post("done")
                        ActivityStack.screenManager.popActivities(this@DailyAddActivity::class.java)
                    }
                )
        }
    }
}
